package com.sne.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sne.common.entity.Result;
import com.sne.common.exception.CustomException;
import com.sne.sys.dto.SysDictDTO;
import com.sne.sys.entity.SysDict;
import com.sne.sys.entity.SysDictItem;
import com.sne.sys.entity.SysUser;
import com.sne.sys.mapper.SysDictMapper;
import com.sne.sys.service.ISysDictItemService;
import com.sne.sys.service.ISysDictService;
import com.sne.sys.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 字典管理 服务实现类
 * </p>
 *
 * @author fanghh
 * @since 2020-01-10
 */
@Slf4j
@Service
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements ISysDictService {

    @Autowired
    private ISysDictItemService iSysDictItemService;

    @Autowired
    private ISysUserService iSysUserService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteDict(Long id) {
        iSysDictItemService.remove(Wrappers.<SysDictItem>query().lambda()
                .eq(SysDictItem::getDictId, id));
        return this.removeById(id);
    }

    @Override
    public IPage<SysDict> findPage(Page page, SysDictDTO dict) {
        SysUser currentUser = iSysUserService.getCurrentUser();
        LambdaQueryWrapper<SysDict> queryWrapper = Wrappers.<SysDict>query().lambda()
                .orderByDesc(SysDict::getCreateTime);
        if (dict.getState()!=null) {
            queryWrapper.eq(SysDict::getState, dict.getState());
        }
        if (StringUtils.isNotBlank(dict.getDictName())) {
            queryWrapper.like(SysDict::getDictName, dict.getDictName());
        }
        if (StringUtils.isNotBlank(dict.getDictCode())) {
            queryWrapper.like(SysDict::getDictCode, dict.getDictCode());
        }
        if (dict.getStartTime() != null) {
            queryWrapper.ge(SysDict::getCreateTime, dict.getStartTime());
        }
        if (dict.getEndTime() != null) {
            queryWrapper.le(SysDict::getCreateTime, dict.getEndTime());
        }
        return this.page(page, queryWrapper);
    }

    @Override
    public boolean checkDictUnique(SysDict dict) {
        SysDict sysDict = this.getOne(Wrappers.<SysDict>query().lambda()
                .eq(SysDict::getDictCode, dict.getDictCode()));
        return (sysDict == null || dict.getId() != null) &&
                (sysDict == null || dict.getId().equals(sysDict.getId()));
    }
}
